﻿using System;
using System.Drawing;
using DevExpress.XtraReports.UI;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using UniMana.Utils.Data;
using UniMana.Data;
using UniMana.Utils.Configs;

namespace UniMana.Win.Training
{
    public partial class RprScoreOfStudentByTermSchoolYearCredits : DevExpress.XtraReports.UI.XtraReport
    {
        private DBMain m_Score = new DBMain();
        private PersonelSignature m_eSignature;
        public enum PersonelSignature
        {
            HieuTruong,
            TruongPhongDaoTao
        }
        public RprScoreOfStudentByTermSchoolYearCredits(string studentID,PersonelSignature signature, bool checkAll = true, 
            int fromTerm=-1, int toTerm=-1, int trainingProgramKey=-1)
        {
            InitializeComponent();
            
            m_eSignature = signature;
            string _strStudentID = studentID;
            int _iProgramKey = trainingProgramKey;
            bool _bCheckAll = checkAll;

            DataTable _dtInfoStudent = new DataTable();
            Training_ScoreInfo infoScore = new Training_ScoreInfo();
            _dtInfoStudent = infoScore.GetInfoStudentByID(_strStudentID).ToDataTable();
            if (_dtInfoStudent.Rows.Count > 0)
            {
                #region [Thong tin sinh vien]
                xrlName.Text = _dtInfoStudent.Rows[0]["Lastname"].ToString().ToUpper() + " " + _dtInfoStudent.Rows[0]["Surename"].ToString().ToUpper();
                DateTime _dteBirthday;
                if (DateTime.TryParse(_dtInfoStudent.Rows[0]["Birthday"].ToString(), out _dteBirthday))
                {
                    xrlBirthday.Text = _dteBirthday.ToString("dd/MM/yyyy");
                }
                else
                {
                    xrlBirthday.Text = _dtInfoStudent.Rows[0]["Birthday"].ToString();
                }
                xrlMajor.Text = _dtInfoStudent.Rows[0]["Major"].ToString().Trim().Replace("Ngành", "").ToUpper();
                xrlStudentId.Text = _dtInfoStudent.Rows[0]["ID"].ToString();
                xrlGender.Text = _dtInfoStudent.Rows[0]["Gender"].ToString().ToUpper();
                xrlLevel.Text = _dtInfoStudent.Rows[0]["Level"].ToString().Trim().Replace("Hệ", "").ToUpper();
                xrlClass.Text = _dtInfoStudent.Rows[0]["ClassName"].ToString().Trim().Replace("Lớp", "").TrimStart();
                xrlPlaceOfBirth.Text = _dtInfoStudent.Rows[0]["PlacesofBirth"].ToString();
                string[] _strCourse = _dtInfoStudent.Rows[0]["Course"].ToString().Trim().Replace("Khóa", "").Split('-');
                xrlCourse.Text = _strCourse[0].ToString().Trim() + " - Nhập học năm " + _strCourse[1].ToString().Trim();
                DateTime date = SysDateTime.Now;
                xrlDate.Text = "Long An, ngày " + date.Day.ToString("00") + " tháng " + date.Month.ToString("00") + " năm " + date.Year.ToString("0000");
                if (signature == PersonelSignature.TruongPhongDaoTao)
                {
                    xrlTCHC.Visible = false;
                }
                #endregion
                #region [Diem]
                #region [Thong tin diem]
                DataTable _dtScore = new DataTable();
                if (checkAll == true)
                {
                    _dtScore = m_Score.m_DCReadOnly.Training_Scores.GetSubjectScoreListofStudentByID(
                                m_Score.m_DCReadOnly.Training_Subjects,
                                m_Score.m_DCReadOnly.Training_TrainingProgram_Details,
                                m_Score.m_DCReadOnly.Training_Student_Subject_Classes,
                                m_Score.m_DCReadOnly.Training_Subject_Classes,
                                m_Score.m_DCReadOnly.Training_Students,
                                m_Score.m_DCReadOnly.Training_Student_Classes,
                                m_Score.m_DCReadOnly.Catalogue_Terms,
                                m_Score.m_DCReadOnly.Catalogue_SchoolYears,
                                _strStudentID, fromTerm, toTerm, _iProgramKey).ToDataTable();
                    if (_dtScore.Rows.Count > 0)
                    {
                        _dtScore.Columns.Add("HPTC");
                        _dtScore.Columns.Add("HPTC4");
                        for (int i = 0; i < _dtScore.Rows.Count; i++)
                        {
                            double _dAverageScore = 0;
                            double _dTotalCredits = 0;
                            if (_dtScore.Rows[i]["AverageScoreFinal"] != null && _dtScore.Rows[i]["AverageScoreFinal"].ToString() != "")
                            {
                                _dAverageScore = Convert.ToDouble(_dtScore.Rows[i]["AverageScoreFinal"].ToString());
                            }
                            if (_dtScore.Rows[i]["TotalCredits"] != null && _dtScore.Rows[i]["TotalCredits"].ToString() != "")
                            {
                                _dTotalCredits = Convert.ToDouble(_dtScore.Rows[i]["TotalCredits"].ToString());
                            }
                            _dtScore.Rows[i]["HPTC"] = (_dAverageScore * _dTotalCredits).ToString();
                            _dtScore.Rows[i]["HPTC4"] = (ChangeScoreNumber(_dAverageScore) * _dTotalCredits).ToString();
                        }
                    }
                }
                else
                {
                    _dtScore = m_Score.m_DCReadOnly.Training_Scores.GetSubjectScoreListofStudentByID(
                                m_Score.m_DCReadOnly.Training_Subjects,
                                m_Score.m_DCReadOnly.Training_TrainingProgram_Details,
                                m_Score.m_DCReadOnly.Training_Student_Subject_Classes,
                                m_Score.m_DCReadOnly.Training_Subject_Classes,
                                m_Score.m_DCReadOnly.Training_Students,
                                m_Score.m_DCReadOnly.Training_Student_Classes,
                                m_Score.m_DCReadOnly.Catalogue_Terms,
                                m_Score.m_DCReadOnly.Catalogue_SchoolYears,
                                _strStudentID, fromTerm, toTerm,_iProgramKey).ToDataTable();
                    if (_dtScore.Rows.Count > 0)
                    {
                        _dtScore.Columns.Add("HPTC");
                        _dtScore.Columns.Add("HPTC4");
                        for (int i = 0; i < _dtScore.Rows.Count; i++)
                        {
                            double _dAverageScore = 0;
                            double _dTotalCredits = 0;
                            if (_dtScore.Rows[i]["AverageScoreFinal"] != null && _dtScore.Rows[i]["AverageScoreFinal"].ToString() != "")
                            {
                                _dAverageScore = Convert.ToDouble(_dtScore.Rows[i]["AverageScoreFinal"].ToString());
                            }
                            if (_dtScore.Rows[i]["TotalCredits"] != null && _dtScore.Rows[i]["TotalCredits"].ToString() != "")
                            {
                                _dTotalCredits = Convert.ToDouble(_dtScore.Rows[i]["TotalCredits"].ToString());
                            }
                            _dtScore.Rows[i]["HPTC"] = (_dAverageScore * _dTotalCredits).ToString();
                            _dtScore.Rows[i]["HPTC4"] = (ChangeScoreNumber(_dAverageScore) * _dTotalCredits).ToString();
                        }
                    }
                }
                #endregion
                #region [Loc danh sach nam hoc]
                List<string> _lCatalogueSchoolYear = new List<string>();
                var _lSchoolYears = (from schoolyear in _dtScore.AsEnumerable()
                                     select schoolyear["SchoolYearName"]).Distinct().ToList();
                foreach (var s in _lSchoolYears)
                {
                    _lCatalogueSchoolYear.Add(s.ToString());
                }
                #endregion
                #region [Bang diem]
                xrTableScore.BeginInit();
                xrTableCell1.Text = "";
                xrTableCell1.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
                int _iSTT = 0;
                double _dTCTL = 0;
                double _dHPTCTL4 = 0;
                double _dHPTCTL10 = 0;
                for (int i = 0; i < _lCatalogueSchoolYear.Count; i++)
                {
                    #region [Loc danh sach hoc ki]
                    //danh sach diem theo nam hoc
                    DataTable _dtScoresBySchoolYear = new DataTable();
                    DataRow[] _drScoreBySchoolYear = _dtScore.Select("[SchoolYearName]='" + _lCatalogueSchoolYear[i].ToString() + "'");
                    _dtScoresBySchoolYear = _dtScore.Clone();
                    foreach (DataRow dr in _drScoreBySchoolYear)
                    {
                        _dtScoresBySchoolYear.ImportRow(dr);
                    }
                    //danh sach hoc ki
                    List<string> _lCatalogueTerm = new List<string>();
                    var _lTerms = (from term in _dtScoresBySchoolYear.AsEnumerable()
                                   select term["TermName"]).Distinct().ToList();
                    foreach (var t in _lTerms)
                    {
                        _lCatalogueTerm.Add(t.ToString());
                    }
                    #endregion
                    #region [Ten column Header]
                    XRTableRow rowTitle = new XRTableRow();
                    XRTableCell cellTitleSTT = new XRTableCell();
                    XRTableCell cellTitleMaMon = new XRTableCell();
                    XRTableCell cellTitleTenMon = new XRTableCell();
                    XRTableCell cellTitleSoTinChi = new XRTableCell();
                    XRTableCell cellTitleHocPhan = new XRTableCell();
                    XRTableCell cellTitleThangDiem4 = new XRTableCell();
                    XRTableCell cellTitleDiemChu = new XRTableCell();
                    XRTableCell cellTitleN = new XRTableCell();
                    XRTableCell cellTitleGhiChu = new XRTableCell();

                    cellTitleSTT.Text = "STT";
                    cellTitleSTT.WidthF = 20;
                    cellTitleSTT.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleSTT.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    cellTitleMaMon.Text = "MÃ MÔN";
                    cellTitleMaMon.WidthF = 40;
                    cellTitleMaMon.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleMaMon.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    cellTitleTenMon.Text = "TÊN MÔN";
                    cellTitleTenMon.WidthF = 155;
                    cellTitleTenMon.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleTenMon.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    cellTitleSoTinChi.Text = "SỐ TC";
                    cellTitleSoTinChi.WidthF = 30;
                    cellTitleSoTinChi.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleSoTinChi.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    cellTitleHocPhan.Text = "TĐ 10";
                    cellTitleHocPhan.WidthF = 30;
                    cellTitleHocPhan.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleHocPhan.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    cellTitleThangDiem4.Text = "TĐ 4";
                    cellTitleThangDiem4.WidthF = 30;
                    cellTitleThangDiem4.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleThangDiem4.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    cellTitleDiemChu.Text = "ĐIỂM CHỮ";
                    cellTitleDiemChu.WidthF = 45;
                    cellTitleDiemChu.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleDiemChu.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    cellTitleN.Text = "N";
                    cellTitleN.WidthF = 10;
                    cellTitleN.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleN.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    cellTitleGhiChu.Text = "GHI CHÚ";
                    cellTitleGhiChu.WidthF = 40;
                    cellTitleGhiChu.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    cellTitleGhiChu.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);

                    rowTitle.Controls.Add(cellTitleSTT);
                    rowTitle.Controls.Add(cellTitleMaMon);
                    rowTitle.Controls.Add(cellTitleTenMon);
                    rowTitle.Controls.Add(cellTitleSoTinChi);
                    rowTitle.Controls.Add(cellTitleHocPhan);
                    rowTitle.Controls.Add(cellTitleThangDiem4);
                    rowTitle.Controls.Add(cellTitleDiemChu);
                    rowTitle.Controls.Add(cellTitleN);
                    rowTitle.Controls.Add(cellTitleGhiChu);
                    rowTitle.Borders = DevExpress.XtraPrinting.BorderSide.All;
                    xrTableScore.Controls.Add(rowTitle);
                    rowTitle.HeightF = 30;
                    #endregion
                    #region [Nam hoc Header]
                    XRTableRow rowSchoolYear = new XRTableRow();
                    XRTableCell cellSchoolYear = new XRTableCell();
                    cellSchoolYear.Text = "NĂM HỌC: " + _lCatalogueSchoolYear[i].ToString();
                    cellSchoolYear.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    rowSchoolYear.Controls.Add(cellSchoolYear);
                    rowSchoolYear.Borders = DevExpress.XtraPrinting.BorderSide.All;
                    xrTableScore.Controls.Add(rowSchoolYear);
                    #endregion
                    double _dTCNH = 0;
                    double _dTCNH_TL = 0;
                    double _dHPTCNH4 = 0;
                    double _dHPTCNH4_TL = 0;
                    double _dHPTCNH10 = 0;
                    double _dHPTCNH10_TL = 0;
                    for (int j = 0; j < _lCatalogueTerm.Count; j++)
                    {
                        #region [Hoc ki Header]
                        XRTableRow rowTermHeader = new XRTableRow();
                        XRTableCell cellTerm = new XRTableCell();
                        cellTerm.Text = "HỌC KÌ: " + _lCatalogueTerm[j].ToString();
                        cellTerm.Font = new System.Drawing.Font(this.Font, FontStyle.Italic);
                        rowTermHeader.Controls.Add(cellTerm);
                        rowTermHeader.Borders = DevExpress.XtraPrinting.BorderSide.All;
                        xrTableScore.Controls.Add(rowTermHeader);
                        #endregion
                        #region [Hoc ki Detail]
                        #region [danh sach diem theo hoc ki]
                        DataTable _dtScoresByTerm = new DataTable();
                        DataRow[] _drScoreByTerm = _dtScoresBySchoolYear.Select("[TermName]='" + _lCatalogueTerm[j].ToString() + "'");
                        _dtScoresByTerm = _dtScoresBySchoolYear.Clone();
                        foreach (DataRow _dr in _drScoreByTerm)
                        {
                            _dtScoresByTerm.ImportRow(_dr);
                        }
                        #endregion
                        //hien thi thong tin
                        double _dTCHK = 0;
                        double _dHPTCHK4 = 0;
                        double _dTCHK_TL = 0;
                        double _dHPTCHK4_TL = 0;
                        for (int k = 0; k < _dtScoresByTerm.Rows.Count; k++)
                        {
                            _iSTT++;
                            #region [thong tin chi tiet]
                            XRTableRow rowContent = new XRTableRow();
                            XRTableCell cellContentSTT = new XRTableCell();
                            XRTableCell cellContentMaMon = new XRTableCell();
                            XRTableCell cellContentTenMon = new XRTableCell();
                            XRTableCell cellContentSoTinChi = new XRTableCell();
                            XRTableCell cellContentHocPhan = new XRTableCell();
                            XRTableCell cellContentThangDiem4 = new XRTableCell();
                            XRTableCell cellContentDiemChu = new XRTableCell();
                            XRTableCell cellContentN = new XRTableCell();
                            XRTableCell cellContentGhiChu = new XRTableCell();
                            //So thu tu
                            cellContentSTT.Text = _iSTT.ToString();
                            cellContentSTT.WidthF = 20;
                            cellContentSTT.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                            //Ma mon
                            if (_dtScoresByTerm.Rows[k]["SubjectID"] != null && _dtScoresByTerm.Rows[k]["SubjectID"].ToString() != "")
                            {
                                cellContentMaMon.Text = _dtScoresByTerm.Rows[k]["SubjectID"].ToString();
                                cellContentMaMon.WidthF = 40;
                                cellContentMaMon.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                            }
                            else
                            {
                                cellContentMaMon.Text = "";
                                cellContentMaMon.WidthF = 40;
                                cellContentMaMon.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                            }
                            //Ten mon
                            if (_dtScoresByTerm.Rows[k]["SubjectName"] != null && _dtScoresByTerm.Rows[k]["SubjectName"].ToString() != "")
                            {
                                cellContentTenMon.Text = _dtScoresByTerm.Rows[k]["SubjectName"].ToString();
                                cellContentTenMon.WidthF = 155;
                                cellContentTenMon.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
                            }
                            else
                            {
                                cellContentTenMon.Text = "";
                                cellContentTenMon.WidthF = 155;
                                cellContentTenMon.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
                            }
                            //Tong tin chi
                            double _dTotalCredits = 0;
                            if (_dtScoresByTerm.Rows[k]["TotalCredits"] != null && _dtScoresByTerm.Rows[k]["TotalCredits"].ToString() != "")
                            {
                                _dTotalCredits = Convert.ToDouble(_dtScoresByTerm.Rows[k]["TotalCredits"].ToString());
                            }
                            cellContentSoTinChi.Text = _dTotalCredits.ToString();
                            cellContentSoTinChi.WidthF = 30;
                            cellContentSoTinChi.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;

                            //Diem trung binh
                            double _dAverageScoreFinal = 0;
                            if (_dtScoresByTerm.Rows[k]["AverageScoreFinal"] != null && _dtScoresByTerm.Rows[k]["AverageScoreFinal"].ToString() != "")
                            {
                                _dAverageScoreFinal = Convert.ToDouble(_dtScoresByTerm.Rows[k]["AverageScoreFinal"].ToString());
                            }
                            cellContentHocPhan.Text = _dAverageScoreFinal.ToString();
                            cellContentHocPhan.WidthF = 30;
                            cellContentHocPhan.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;

                            cellContentThangDiem4.Text = ChangeScoreNumber(_dAverageScoreFinal).ToString();
                            cellContentThangDiem4.WidthF = 30;
                            cellContentThangDiem4.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;

                            cellContentDiemChu.Text = ChangeScoreLetter(_dAverageScoreFinal);
                            cellContentDiemChu.WidthF = 45;
                            cellContentDiemChu.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;

                            cellContentN.Text = "";
                            cellContentN.WidthF = 10;
                            cellContentN.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;

                            if (_dAverageScoreFinal >= DefaultConfig.MinScorePass_NoYearly)
                            {
                                cellContentGhiChu.Text = "";
                                cellContentGhiChu.WidthF = 40;
                                cellContentGhiChu.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                            }
                            else
                            {
                                cellContentGhiChu.Text = "Không đạt";
                                cellContentGhiChu.WidthF = 40;
                                cellContentGhiChu.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                            }

                            rowContent.Controls.Add(cellContentSTT);
                            rowContent.Controls.Add(cellContentMaMon);
                            rowContent.Controls.Add(cellContentTenMon);
                            rowContent.Controls.Add(cellContentSoTinChi);
                            rowContent.Controls.Add(cellContentHocPhan);
                            rowContent.Controls.Add(cellContentThangDiem4);
                            rowContent.Controls.Add(cellContentDiemChu);
                            rowContent.Controls.Add(cellContentN);
                            rowContent.Controls.Add(cellContentGhiChu);
                            rowContent.Borders = DevExpress.XtraPrinting.BorderSide.All;
                            xrTableScore.Controls.Add(rowContent);
                            #endregion
                            if (_dAverageScoreFinal >= DefaultConfig.MinScorePass_NoYearly)
                            {
                                //So tin chi - Diem  tich luy chung toan khoa
                                _dTCTL += _dTotalCredits;
                                _dHPTCTL4 += Convert.ToDouble(_dtScoresByTerm.Rows[k]["HPTC4"].ToString());
                                _dHPTCTL10 += Convert.ToDouble(_dtScoresByTerm.Rows[k]["HPTC"].ToString());
                                //So tin chi - Diem tich luy chung hoc ki
                                _dTCHK_TL += _dTotalCredits;
                                _dHPTCHK4_TL += Convert.ToDouble(_dtScoresByTerm.Rows[k]["HPTC4"].ToString());
                                //So tin chi - Diem tich luy chung nam hoc
                                _dTCNH_TL += _dTotalCredits;
                                _dHPTCNH4_TL += Convert.ToDouble(_dtScoresByTerm.Rows[k]["HPTC4"].ToString());
                                _dHPTCNH10_TL += Convert.ToDouble(_dtScoresByTerm.Rows[k]["HPTC"].ToString());
                            }
                            _dTCHK += _dTotalCredits;
                            _dHPTCHK4 += Convert.ToDouble(_dtScoresByTerm.Rows[k]["HPTC4"].ToString());
                            _dHPTCNH10 += Convert.ToDouble(_dtScoresByTerm.Rows[k]["HPTC"].ToString());
                        }
                        #endregion
                        double _dTBHK = Math.Round(_dHPTCHK4 / _dTCHK, 2,MidpointRounding.AwayFromZero);
                        #region [Hoc ki Footer]
                        XRTableRow rowTermFooter = new XRTableRow();
                        XRTableCell cellTermTB = new XRTableCell();
                        XRTableCell cellTermXL = new XRTableCell();
                        XRTableCell cellTermTongTC = new XRTableCell();
                        XRTableCell cellTermTBTL = new XRTableCell();
                        XRTableCell cellTermTCTL = new XRTableCell();

                        cellTermTB.Text = "\tTBHK: " + _dTBHK.ToString("0.00");
                        cellTermTB.Font = new System.Drawing.Font(this.Font, FontStyle.Italic);
                        cellTermTB.WidthF = 125;
                        //cellTermTB.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopRight;
                        //cellTermTB.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                        cellTermTB.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom;

                        //cellTermXL.Text = "XLHK: "+ RankCredits(ChangeScoreNumber(_dTBHK));
                        //cellTermXL.Font = new System.Drawing.Font(this.Font, FontStyle.Italic);
                        //cellTermXL.WidthF = 160;
                        //cellTermXL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopRight;
                        //cellTermXL.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;

                        cellTermTongTC.Text = "Số TCHK: " + _dTCHK.ToString();
                        cellTermTongTC.Font = new System.Drawing.Font(this.Font, FontStyle.Italic);
                        cellTermTongTC.WidthF = 150;
                        //cellTermTongTC.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopRight;
                        cellTermTongTC.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                        cellTermTongTC.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;


                        if (_dTCHK_TL != 0)
                        {
                            cellTermTBTL.Text = "TBCTL: " + Math.Round(_dHPTCHK4_TL / _dTCHK_TL, 2, MidpointRounding.AwayFromZero).ToString("0.00");
                            cellTermTBTL.Font = new System.Drawing.Font(this.Font, FontStyle.Italic);
                            cellTermTBTL.WidthF = 100;
                            //cellTermTBTL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopRight;
                            cellTermTBTL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                            cellTermTBTL.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
                        }
                        else
                        {
                            cellTermTBTL.Text = "TBTL: 0.00";
                            cellTermTBTL.Font = new System.Drawing.Font(this.Font, FontStyle.Italic);
                            cellTermTBTL.WidthF = 100;
                            cellTermTBTL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                            cellTermTBTL.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
                        }

                        cellTermTCTL.Text = "Tổng số TCTL: " + _dTCHK_TL.ToString();
                        cellTermTCTL.Font = new System.Drawing.Font(this.Font, FontStyle.Italic);
                        //cellTermTCTL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopRight;
                        cellTermTCTL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                        cellTermTCTL.Borders = DevExpress.XtraPrinting.BorderSide.None;
                        cellTermTCTL.Borders = DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom;

                        rowTermFooter.Controls.Add(cellTermTB);
                        //rowTermFooter.Controls.Add(cellTermXL);
                        rowTermFooter.Controls.Add(cellTermTongTC);
                        rowTermFooter.Controls.Add(cellTermTBTL);
                        rowTermFooter.Controls.Add(cellTermTCTL);
                        xrTableScore.Controls.Add(rowTermFooter);
                        rowTermFooter.Borders = DevExpress.XtraPrinting.BorderSide.All;

                        XRTableRow rowTermFooterDRL = new XRTableRow();
                        XRTableCell cellTermDRL = new XRTableCell();
                        cellTermDRL.Text = "\tĐRL: ";
                        cellTermDRL.Font = new System.Drawing.Font(this.Font, FontStyle.Italic);
                        rowTermFooterDRL.Controls.Add(cellTermDRL);
                        xrTableScore.Controls.Add(rowTermFooterDRL);
                        rowTermFooterDRL.Borders = DevExpress.XtraPrinting.BorderSide.All;
                        #endregion
                        _dTCNH += _dTCHK;
                        _dHPTCNH4 += _dHPTCHK4;
                    }
                    double _dTBNH = Math.Round(_dHPTCNH4 / _dTCNH, 2,MidpointRounding.AwayFromZero);
                    #region [Nam hoc Footer TĐ10]
                    XRTableRow rowFooterSchoolYearTD10 = new XRTableRow();

                    XRTableCell cellSchoolYearTBTD10 = new XRTableCell();
                    XRTableCell cellSchoolYearTongTCTD10 = new XRTableCell();
                    XRTableCell cellSchoolYearTBTLTD10 = new XRTableCell();
                    XRTableCell cellSchoolYearTCTLTD10 = new XRTableCell();

                    cellSchoolYearTBTD10.Text = "Trung bình chung năm học(TĐ10): " + Math.Round(_dHPTCNH10 / _dTCNH, 2,MidpointRounding.AwayFromZero).ToString("0.00");
                    cellSchoolYearTBTD10.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    cellSchoolYearTBTD10.WidthF = 170;
                    //cellSchoolYearTBTD10.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                    cellSchoolYearTBTD10.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom;


                    cellSchoolYearTongTCTD10.Text = "Số TC năm học: " + _dTCNH.ToString();
                    cellSchoolYearTongTCTD10.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    cellSchoolYearTongTCTD10.WidthF = 90;
                    cellSchoolYearTongTCTD10.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                    cellSchoolYearTongTCTD10.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;

                    if (_dTCNH_TL != 0)
                    {
                        cellSchoolYearTBTLTD10.Text = "Trung bình chung tích lũy: " + Math.Round(_dHPTCNH10_TL / _dTCNH_TL, 2,MidpointRounding.AwayFromZero).ToString("0.00");
                        cellSchoolYearTBTLTD10.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                        cellSchoolYearTBTLTD10.WidthF = 125;
                        cellSchoolYearTBTLTD10.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
                        cellSchoolYearTBTLTD10.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
                    }
                    else
                    {
                        cellSchoolYearTBTLTD10.Text = "Trung bình chung tích lũy: 0.00";
                        cellSchoolYearTBTLTD10.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                        cellSchoolYearTBTLTD10.WidthF = 125;
                        cellSchoolYearTBTLTD10.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
                        cellSchoolYearTBTLTD10.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
                    }

                    cellSchoolYearTCTLTD10.Text = "Số TC tích lũy: " + _dTCNH_TL.ToString();
                    cellSchoolYearTCTLTD10.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    cellSchoolYearTCTLTD10.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                    cellSchoolYearTCTLTD10.Borders = DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom;

                    rowFooterSchoolYearTD10.Controls.Add(cellSchoolYearTBTD10);
                    rowFooterSchoolYearTD10.Controls.Add(cellSchoolYearTongTCTD10);
                    rowFooterSchoolYearTD10.Controls.Add(cellSchoolYearTBTLTD10);
                    rowFooterSchoolYearTD10.Controls.Add(cellSchoolYearTCTLTD10);
                    xrTableScore.Controls.Add(rowFooterSchoolYearTD10);
                    #endregion
                    #region [Nam hoc Footer TĐ4]
                    XRTableRow rowFooterSchoolYear = new XRTableRow();

                    XRTableCell cellSchoolYearTB = new XRTableCell();
                    XRTableCell cellSchoolYearXL = new XRTableCell();
                    XRTableCell cellSchoolYearTongTC = new XRTableCell();
                    XRTableCell cellSchoolYearTBTL = new XRTableCell();
                    XRTableCell cellSchoolYearTCTL = new XRTableCell();

                    cellSchoolYearTB.Text = "Trung bình chung năm học(TĐ4)  : " + _dTBNH.ToString("0.00");
                    cellSchoolYearTB.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    cellSchoolYearTB.WidthF = 170;
                    //cellSchoolYearTB.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                    cellSchoolYearTB.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom;

                    //cellSchoolYearXL.Text = "XLNH: " + RankCredits(ChangeScoreNumber(_dTBNH));
                    //cellSchoolYearXL.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    //cellSchoolYearXL.WidthF = 160;
                    //cellSchoolYearXL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopRight;
                    //cellSchoolYearXL.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;

                    cellSchoolYearTongTC.Text = "Số TC năm học: " + _dTCNH.ToString() ;
                    cellSchoolYearTongTC.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    cellSchoolYearTongTC.WidthF = 90;
                    cellSchoolYearTongTC.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                    cellSchoolYearTongTC.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;

                    if (_dTCNH_TL != 0)
                    {
                        cellSchoolYearTBTL.Text = "Trung bình chung tích lũy: " + Math.Round(_dHPTCNH4_TL / _dTCNH_TL, 2,MidpointRounding.AwayFromZero).ToString("0.00");
                        cellSchoolYearTBTL.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                        cellSchoolYearTBTL.WidthF = 125;
                        cellSchoolYearTBTL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
                        cellSchoolYearTBTL.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
                    }
                    else
                    {
                        cellSchoolYearTBTL.Text = "Trung bình chung tích lũy: 0.00";
                        cellSchoolYearTBTL.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                        cellSchoolYearTBTL.WidthF = 125;
                        cellSchoolYearTBTL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
                        cellSchoolYearTBTL.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
                    }

                    cellSchoolYearTCTL.Text = "Số TC tích lũy: " + _dTCNH_TL.ToString();
                    cellSchoolYearTCTL.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    cellSchoolYearTCTL.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
                    cellSchoolYearTCTL.Borders = DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom;

                    rowFooterSchoolYear.Controls.Add(cellSchoolYearTB);
                    //rowFooterSchoolYear.Controls.Add(cellSchoolYearXL);
                    rowFooterSchoolYear.Controls.Add(cellSchoolYearTongTC);
                    rowFooterSchoolYear.Controls.Add(cellSchoolYearTBTL);
                    rowFooterSchoolYear.Controls.Add(cellSchoolYearTCTL);
                    xrTableScore.Controls.Add(rowFooterSchoolYear);

                    //Diem ren luyen
                    XRTableRow rowSchoolYearFooterDRL = new XRTableRow();
                    XRTableCell cellSchoolYearDRL = new XRTableCell();
                    cellSchoolYearDRL.Text = "Điểm rèn luyện: ";
                    cellSchoolYearDRL.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                    cellSchoolYearDRL.Borders = DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom;
                    rowSchoolYearFooterDRL.Controls.Add(cellSchoolYearDRL);
                    xrTableScore.Controls.Add(rowSchoolYearFooterDRL);
                    #endregion
                }
                #region [Diem trung binh & xep loai cuoi khoa]
                Training_TrainingProgramInfo infoProgram = new Training_TrainingProgramInfo(_iProgramKey);
                if (infoProgram.Properties.GradCredits != null && infoProgram.Properties.GradCredits.ToString() != "")
                {
                    if (_dTCTL >= infoProgram.Properties.GradCredits)
                    {
                        //Diem trung binh cuoi khoa thang diem 10
                        XRTableRow rowTBFinalTD10 = new XRTableRow();
                        XRTableCell cellTBFinalTD10 = new XRTableCell();
                        cellTBFinalTD10.Text = "TRUNG BÌNH CHUNG(TĐ4): " + Math.Round(_dHPTCTL10 / _dTCTL, 2, MidpointRounding.AwayFromZero).ToString("0.00");
                        cellTBFinalTD10.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                        rowTBFinalTD10.Controls.Add(cellTBFinalTD10);
                        rowTBFinalTD10.Borders = DevExpress.XtraPrinting.BorderSide.All;
                        xrTableScore.Controls.Add(rowTBFinalTD10);
                        //Diem trung binh cuoi khoa thang diem 4
                        XRTableRow rowTBFinal = new XRTableRow();
                        XRTableCell cellTBFinal = new XRTableCell();
                        cellTBFinal.Text = "TRUNG BÌNH CHUNG(TĐ4): " + Math.Round(_dHPTCTL4 / _dTCTL, 2, MidpointRounding.AwayFromZero).ToString("0.00");
                        cellTBFinal.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                        rowTBFinal.Controls.Add(cellTBFinal);
                        rowTBFinal.Borders = DevExpress.XtraPrinting.BorderSide.All;
                        xrTableScore.Controls.Add(rowTBFinal);
                        //Xep loai cuoi khoa
                        XRTableRow rowXLFinal = new XRTableRow();
                        XRTableCell cellXLFinal = new XRTableCell();
                        cellXLFinal.Text = "XẾP LOẠI: " + RankCredits(Math.Round(_dHPTCTL4 / _dTCTL, 2, MidpointRounding.AwayFromZero));
                        cellXLFinal.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);
                        rowXLFinal.Controls.Add(cellXLFinal);
                        rowXLFinal.Borders = DevExpress.XtraPrinting.BorderSide.All;
                        xrTableScore.Controls.Add(rowXLFinal);
                    }
                }
                #endregion
                xrTableScore.EndInit();
                #endregion
                #endregion
            }
            else
            {
                //XtraMessageBox.Show("Mã sinh viên không tồn tại!","Thông báo");
            }
        }
        private string Rank(double score)
        {
            if (score >= 9)
            {
                return " Xuất sắc ";
            }
            else if (score >= 8)
            {
                return " Giỏi ";
            }
            else if (score >= 7)
            {
                return " Khá ";
            }
            else if (score >= 6)
            {
                return " TB Khá ";
            }
            else if (score >= 5)
            {
                return " TB ";
            }
            else if (score >= 3)
            {
                return " Yếu ";
            }
            else
            {
                return " Kém ";
            }
        }
        private string RankCredits(double score)
        {
            if (score >= 3.6)
            {
                return " Xuất sắc ";
            }
            else if (score >= 3.2)
            {
                return " Giỏi ";
            }
            else if (score >= 2.50)
            {
                return " Khá ";
            }
            else if (score >= 2)
            {
                return " Trung bình ";
            }
            else
            {
                return "";
            }
        }
        private double ChangeScoreNumber(double score)
        {
            if (score >= 8.5)
            {
                return 4.00;
            }
            else if (score >= 8)
            {
                return 3.50;
            }
            else if (score >= 7)
            {
                return 3.00;
            }
            else if (score >= 6.5)
            {
                return 2.50;
            }
            else if (score >= 5.5)
            {
                return 2.00;
            }
            else if (score >= 5)
            {
                return 1.50;
            }
            else if (score >= 4)
            {
                return 1.00;
            }
            else
            {
                return 0.00;
            }
        }
        private string ChangeScoreLetter(double score)
        {
            if (score >= 8.5)
            {
                return "A";
            }
            else if (score >= 8)
            {
                return "B+";
            }
            else if (score >= 7)
            {
                return "B";
            }
            else if (score >= 6.5)
            {
                return "C+";
            }
            else if (score >= 5.5)
            {
                return "C";
            }
            else if (score >= 5)
            {
                return "D+";
            }
            else if (score >= 4)
            {
                return "D";
            }
            else
            {
                return "F";
            }
        }

    }
}

